package org.mogu.tools;

import java.util.ArrayList;
import java.util.Date;

import org.mogu.service.InfoSync;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AppUrlMap {

	public static ArrayList<String> getAppUrlByCode(String uname, String code,
			SQLiteDatabase readdb) {
		ArrayList<String> appMap = new ArrayList<String>();
		String[] appAr = { uname, code };
		synchronized (InfoSync.class) {
			Cursor app = readdb.rawQuery(
					"select url from app where username=? and code=?", appAr);

			app.moveToFirst();
			while (!app.isAfterLast()) {

				appMap.add(app.getString(0));
				app.moveToNext();
			}
			app.close();
		}
		return appMap;
	}
	public static String getAppParamByCode(String uname, 
			SQLiteDatabase readdb) {
		String appMap = "";
		String[] appAr = { uname };
		synchronized (InfoSync.class) {
			Cursor app = readdb.rawQuery(
					"select appparam from activeuser where username=?", appAr);
			
			app.moveToFirst();
			while (!app.isAfterLast()) {
				
				appMap=app.getString(0);
				app.moveToNext();
			}
			app.close();
		}
		return appMap;
	}
	public static String getUserParamByCode(String uname, String code,
			SQLiteDatabase readdb) {
		String appMap = "";
		String[] appAr = { uname, code };
		synchronized (InfoSync.class) {
			Cursor app = readdb.rawQuery(
					"select appparm from userapp where username=? and code=?", appAr);
			
			app.moveToFirst();
			while (!app.isAfterLast()) {
				
				appMap=app.getString(0);
				app.moveToNext();
			}
			app.close();
		}
		return appMap;
	}
	
	public static String getParamByCode(String uname, String code,
			SQLiteDatabase readdb) {
		return getAppParamByCode(uname,readdb)+getUserParamByCode(uname,code,readdb);
	}
	
	
	public static String getUrl(ArrayList<String> appMap){
		if(appMap.size()==0){
			return "";
		}
		return appMap.get(new Date().getSeconds()% appMap.size());
	}

}
